"""Test sensor entity for HomeWizard."""

from unittest.mock import MagicMock

from homewizard_energy.errors import RequestError
from homewizard_energy.models import CombinedModels, Measurement, State, System
import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.homewizard.const import UPDATE_INTERVAL
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt as dt_util

from tests.common import async_fire_time_changed

pytestmark = [
    pytest.mark.usefixtures("init_integration"),
]


@pytest.mark.freeze_time("2025-01-28 21:45:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize(
    ("device_fixture", "entity_ids"),
    [
        (
            "HWE-P1",
            [
                "sensor.device_average_demand",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_export",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
                "sensor.gas_meter_gas",
                "sensor.heat_meter_energy",
                "sensor.inlet_heat_meter_none",
                "sensor.warm_water_meter_water",
                "sensor.water_meter_water",
            ],
        ),
        (
            "HWE-P1-zero-values",
            [
                "sensor.device_average_demand",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_export",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_total_water_usage",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-SKT-11",
            [
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_power_phase_1",
                "sensor.device_power",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-SKT-21",
            [
                "sensor.device_apparent_power",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power_factor",
                "sensor.device_power_phase_1",
                "sensor.device_power",
                "sensor.device_reactive_power",
                "sensor.device_voltage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-WTR",
            [
                "sensor.device_total_water_usage",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "SDM230",
            [
                "sensor.device_apparent_power",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power_factor",
                "sensor.device_power",
                "sensor.device_reactive_power",
                "sensor.device_voltage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "SDM630",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-KWH1",
            [
                "sensor.device_apparent_power",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power_factor",
                "sensor.device_power",
                "sensor.device_reactive_power",
                "sensor.device_voltage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-KWH3",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-P1-invalid-EAN",
            [
                "sensor.device_average_demand",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_export",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_ssid",
                "sensor.device_wi_fi_strength",
                "sensor.gas_meter_gas",
                "sensor.heat_meter_energy",
                "sensor.inlet_heat_meter_none",
                "sensor.warm_water_meter_water",
                "sensor.water_meter_water",
            ],
        ),
        (
            "HWE-BAT",
            [
                "sensor.device_battery_cycles",
                "sensor.device_current",
                "sensor.device_energy_export",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_power",
                "sensor.device_state_of_charge",
                "sensor.device_uptime",
                "sensor.device_voltage",
                "sensor.device_wi_fi_rssi",
                "sensor.device_wi_fi_ssid",
            ],
        ),
    ],
)
async def test_sensors(
    hass: HomeAssistant,
    device_registry: dr.DeviceRegistry,
    entity_registry: er.EntityRegistry,
    snapshot: SnapshotAssertion,
    entity_ids: list[str],
) -> None:
    """Test that sensor entity snapshots match."""
    for entity_id in entity_ids:
        assert (state := hass.states.get(entity_id))
        assert snapshot(name=f"{entity_id}:state") == state

        assert (entity_entry := entity_registry.async_get(state.entity_id))
        assert snapshot(name=f"{entity_id}:entity-registry") == entity_entry

        assert entity_entry.device_id
        assert (device_entry := device_registry.async_get(entity_entry.device_id))
        assert snapshot(name=f"{entity_id}:device-registry") == device_entry


@pytest.mark.parametrize(
    ("device_fixture", "entity_ids"),
    [
        (
            "HWE-P1",
            [
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_frequency",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-P1-unused-exports",
            [
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_export",
            ],
        ),
        (
            "HWE-SKT-11",
            [
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-SKT-21",
            [
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-WTR",
            [
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "SDM230",
            [
                "sensor.device_apparent_power",
                "sensor.device_current",
                "sensor.device_frequency",
                "sensor.device_power_factor",
                "sensor.device_reactive_power",
                "sensor.device_voltage",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "SDM630",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_frequency",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-KWH1",
            [
                "sensor.device_apparent_power",
                "sensor.device_current",
                "sensor.device_frequency",
                "sensor.device_power_factor",
                "sensor.device_reactive_power",
                "sensor.device_voltage",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-KWH3",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_frequency",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_wi_fi_strength",
            ],
        ),
        (
            "HWE-BAT",
            [
                "sensor.device_current",
                "sensor.device_frequency",
                "sensor.device_uptime",
                "sensor.device_voltage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
    ],
)
async def test_disabled_by_default_sensors(
    hass: HomeAssistant, entity_registry: er.EntityRegistry, entity_ids: list[str]
) -> None:
    """Test the disabled by default sensors."""
    for entity_id in entity_ids:
        assert not hass.states.get(entity_id)

        assert (entry := entity_registry.async_get(entity_id))
        assert entry.disabled
        assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION


@pytest.mark.parametrize("exception", [RequestError])
async def test_sensors_unreachable(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
    exception: Exception,
) -> None:
    """Test sensor handles API unreachable."""
    assert (state := hass.states.get("sensor.device_energy_import_tariff_1"))
    assert state.state == "10830.511"

    mock_homewizardenergy.combined.side_effect = exception
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    assert (state := hass.states.get(state.entity_id))
    assert state.state == STATE_UNAVAILABLE


@pytest.mark.parametrize("exception", [RequestError])
async def test_external_sensors_unreachable(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
    exception: Exception,
) -> None:
    """Test external device sensor handles API unreachable."""
    assert (state := hass.states.get("sensor.gas_meter_gas"))
    assert state.state == "111.111"

    mock_homewizardenergy.combined.side_effect = exception
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    assert (state := hass.states.get(state.entity_id))
    assert state.state == STATE_UNAVAILABLE


@pytest.mark.parametrize(
    ("device_fixture", "entity_ids"),
    [
        (
            "HWE-SKT-11",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_factor",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_voltage",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "HWE-SKT-21",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "HWE-WTR",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_current",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_export",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_energy_import",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_factor",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_power",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_voltage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "SDM230",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_average_demand",
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "SDM630",
            [
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_failures_detected",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_voltage",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "HWE-KWH1",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_average_demand",
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_2",
                "sensor.device_power_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "HWE-KWH3",
            [
                "sensor.device_average_demand",
                "sensor.device_battery_cycles",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_3",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_frequency",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_failures_detected",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_state_of_charge",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_uptime",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_voltage",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_rssi",
            ],
        ),
        (
            "HWE-BAT",
            [
                "sensor.device_apparent_power_phase_1",
                "sensor.device_apparent_power_phase_2",
                "sensor.device_apparent_power_phase_3",
                "sensor.device_apparent_power",
                "sensor.device_average_demand",
                "sensor.device_current_phase_1",
                "sensor.device_current_phase_2",
                "sensor.device_current_phase_3",
                "sensor.device_dsmr_version",
                "sensor.device_energy_export_tariff_1",
                "sensor.device_energy_export_tariff_2",
                "sensor.device_energy_export_tariff_4",
                "sensor.device_energy_import_tariff_1",
                "sensor.device_energy_import_tariff_2",
                "sensor.device_energy_import_tariff_3",
                "sensor.device_energy_import_tariff_4",
                "sensor.device_long_power_failures_detected",
                "sensor.device_peak_demand_current_month",
                "sensor.device_power_factor_phase_1",
                "sensor.device_power_factor_phase_2",
                "sensor.device_power_factor_phase_3",
                "sensor.device_power_factor",
                "sensor.device_power_failures_detected",
                "sensor.device_power_phase_1",
                "sensor.device_power_phase_3",
                "sensor.device_reactive_power_phase_1",
                "sensor.device_reactive_power_phase_2",
                "sensor.device_reactive_power_phase_3",
                "sensor.device_reactive_power",
                "sensor.device_smart_meter_identifier",
                "sensor.device_smart_meter_model",
                "sensor.device_tariff",
                "sensor.device_total_water_usage",
                "sensor.device_voltage_phase_1",
                "sensor.device_voltage_phase_2",
                "sensor.device_voltage_phase_3",
                "sensor.device_voltage_sags_detected_phase_1",
                "sensor.device_voltage_sags_detected_phase_2",
                "sensor.device_voltage_sags_detected_phase_3",
                "sensor.device_voltage_swells_detected_phase_1",
                "sensor.device_voltage_swells_detected_phase_2",
                "sensor.device_voltage_swells_detected_phase_3",
                "sensor.device_water_usage",
                "sensor.device_wi_fi_strength",
            ],
        ),
    ],
)
async def test_entities_not_created_for_device(
    hass: HomeAssistant,
    entity_ids: list[str],
) -> None:
    """Ensures entities for a specific device are not created."""
    for entity_id in entity_ids:
        assert not hass.states.get(entity_id)


@pytest.mark.parametrize("device_fixture", ["HWE-BAT"])
@pytest.mark.freeze_time("2021-01-01 12:00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_uptime_sensor_does_not_update_timestamp_on_data_update(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
) -> None:
    """Test that the uptime sensor does not update its timestamp when refreshing data."""
    entity_id = "sensor.device_uptime"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=356),
        state=State(),
    )

    # Initial state
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=356 + UPDATE_INTERVAL.seconds),
        state=State(),
    )

    # Uptime should be the same after the initial setup
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    # Check that the uptime sensor has updated
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"


@pytest.mark.parametrize("device_fixture", ["HWE-BAT"])
@pytest.mark.freeze_time("2021-01-01 12:00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_uptime_sensor_does_not_update_timestamp_on_minor_change(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
) -> None:
    """Test that the uptime sensor does not update its timestamp on minor changes."""
    entity_id = "sensor.device_uptime"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=356),
        state=State(),
    )

    # Initial state
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=400 + UPDATE_INTERVAL.seconds),
        state=State(),
    )

    # Uptime should be the same after the initial setup
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    # Check that the uptime sensor has updated
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"


@pytest.mark.parametrize("device_fixture", ["HWE-BAT"])
@pytest.mark.freeze_time("2021-01-01 12:00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_uptime_sensor_refreshes_when_detecting_reboot(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
) -> None:
    """Test that the uptime sensor updates its timestamp on reboot."""
    entity_id = "sensor.device_uptime"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=356),
        state=State(),
    )

    # Initial state
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None, measurement=Measurement(), system=System(uptime_s=0), state=State()
    )

    # Simulate a reboot by setting uptime to 0, timestamp should update
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    # Check that the uptime sensor has updated
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T12:00:00+00:00"


@pytest.mark.parametrize("device_fixture", ["HWE-BAT"])
@pytest.mark.freeze_time("2021-01-01 12:00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_uptime_sensor_unavailable(
    hass: HomeAssistant,
    mock_homewizardenergy: MagicMock,
) -> None:
    """Test that the uptime sensor reports unavailable when uptime is None."""
    entity_id = "sensor.device_uptime"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=356),
        state=State(),
    )

    # Initial state
    assert (state := hass.states.get(entity_id))
    assert state.state == "2021-01-01T11:54:04+00:00"

    mock_homewizardenergy.combined.return_value = CombinedModels(
        device=None,
        measurement=Measurement(),
        system=System(uptime_s=None),
        state=State(),
    )

    # Uptime should be the same after the initial setup
    async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
    await hass.async_block_till_done()

    # Check that the uptime sensor has updated
    assert (state := hass.states.get(entity_id))
    assert state.state == STATE_UNAVAILABLE
